import Vuex from 'vuex'
import Vue from 'vue'
import $ from '@/common'
import {
  nodeParamParser
} from '@/common/parser'
Vue.use(Vuex)

const state = {
  path: {
    nodes: 'service/{app}/nodes?account_code={account_code}'
  },
  activePage: null,
  maxTabs: 5,
  pages: [],
  // 记录当前节点树形结构
  modulesTree: {
    node: {}
  },
  user: {},
  nodes: {},
  nodesLoad: false
}
const actions = {
  fetchNodes({
    state,
    commit,
    dispatch
  }, data) {
    return new Promise((resolve, reject) => {
      if (!data) {
        $.link(state.path.nodes, null, {
          param: state.user
        }).then((res) => {
          resolve(res.data)
        }).catch((res) => {
          reject(res)
        })
      } else {
        resolve(data)
      }
    }).then((res) => {
      if ($.dev) {
        // 开发模式下附加开发工具节点
        require(['../dev/extNodes'], (data) => {
          dispatch('loadNodes', [...res, ...data.default])
        })
      } else {
        dispatch('loadNodes', res)
      }
    })
  },
  loadNodes({
    state,
    commit
  }, data) {
    var list = {}
    commit('nodesLoad', false)

    data.forEach((node) => {
      node.param = nodeParamParser(node)
      node.code_short = $.getNodeCode(node.code)
      list[node.code_short] = node
      if (node.children) {
        node.children.forEach((child) => {
          child.param = nodeParamParser(child)
          child.code_short = $.getNodeCode(child.code)
          child.parent_code = node.code_short
          list[child.code_short] = child
        })
      }
    })
    commit('setNodes', data)
    $.nodeData = data
    $.nodeList = list
    commit('nodesLoad', true)
  }
}

const mutations = {
  // 将当前页面节点加入队列中
  pushPage(state, item) {
    for (let i in state.pages) {
      let page = state.pages[i]
      if (page.code && page.code === item.code) {
        state.activePage = page.code
        return
      }
    }
    if (state.pages.length >= state.maxTabs) {
      state.pages.pop()
    }
    state.pages.unshift({
      ...item
    })
    state.activePage = item.code
  },
  setPageModel(state, {
    data,
    code
  }) {
    for (let i = 0; i < state.pages.length; i++) {
      let page = state.pages[i]
      if (page.code && (page.code === code || page.code_short === code)) {
        if (!page.model) {
          page.model = data
        }
        break
      }
    }
  },
  closePage(state, code) {
    for (let i = 0; i < state.pages.length; i++) {
      let page = state.pages[i]
      if (page.code && (page.code === code || page.code_short === code)) {
        state.pages.splice(i, 1)
        break
      }
    }
  },
  //缓存状态更新使用浅拷贝，同样的键名就会被覆盖，避免状态数据的叠加
  updatePageStatus(state, {
    data,
    code,
    index
  }) {
    for (let i = 0; i < state.pages.length; i++) {
      let page = state.pages[i]
      if (page.code && (page.code === code || page.code_short === code)) {
        page.data = page.data || {}
        if (index !== null && index !== undefined) {
          page.data.facets = page.data.facets || []
          page.data.facets[index] = page.data.facets[index] || {}
          $.set(page.data.facets, index, {
            ...page.data.facets[index],
            ...data
          })
        } else {
          $.set(page, 'data', { ...page.data,
            ...data
          })
        }
        return
      }
    }
  },
  addModules(state, path) {},
  // 更新节点结构
  updateTree(state, path) {},
  setUser(state, data) {
    Vue.set(state, 'user', data)
  },
  setNodes(state, data) {
    Vue.set(state, 'nodes', data)
  },
  nodesLoad(state, value) {
    state.nodesLoad = value
  },
  clearAuth(state, data) {},
  clearNode(state) {},
  uninstallNode(state, path) {}
}

const getters = {
  activePageData(state) {
    for (let i = 0; i < state.pages.length; i++) {
      let page = state.pages[i]
      if (page.code && (page.code === state.activePage || page.code_short === state.activePage)) {
        return page
      }
    }
  }
}

export default new Vuex.Store({
  // strict: true,
  state,
  getters,
  actions,
  mutations
})
